package com.drinks.yiniang.io.api;

import com.alibaba.fastjson.JSON;
import com.drinks.yiniang.entity.user.points.CouponJson;
import com.drinks.yiniang.entity.user.points.PointsExchangeJson;
import com.drinks.yiniang.io.http.HttpUrls;
import com.hansen.library.entity.BaseJson;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.utils.LogUtils;

/**
 * 优惠券api
 * Created by han on 2019/3/6 0006.
 */

public class CouponApiIO extends BaseApiIO {
    volatile static CouponApiIO instance;

    static public CouponApiIO getInstance() {
        if (instance == null) {
            synchronized (CouponApiIO.class) {
                if (instance == null) {
                    instance = new CouponApiIO();
                }
            }
        }
        return instance;
    }

    /**
     * 获取优惠券列表
     *
     * @param  type     类型 0通用 1门店
     * @param callback
     */
    public void getCouponList(String type, final APIRequestCallback<CouponJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("type", type);

        MainApiIO.getInstance().postRequest(HttpUrls.API_COUPON_LIST, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("获取优惠券列表===onSuccess");

                CouponJson mDataJson = JSON.parseObject(res, CouponJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 获取优惠券兑换列表
     *
     * @param callback
     */
    public void getCouponExchangeList(final APIRequestCallback<PointsExchangeJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();

        MainApiIO.getInstance().postRequest(HttpUrls.API_COUPON_EXCHANGE_LIST, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("获取优惠券兑换列表===onSuccess");

                PointsExchangeJson mDataJson = JSON.parseObject(res, PointsExchangeJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null && mDataJson.getData().getCoupons() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 优惠券兑换
     *
     * @param couponId 优惠券id
     * @param callback
     */
    public void doExchangeCoupon(String couponId, final APIRequestCallback<BaseJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("coupons_id", couponId);

        MainApiIO.getInstance().postRequest(HttpUrls.API_COUPON_EXCHANGE, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("优惠券兑换===onSuccess");

                BaseJson mDataJson = JSON.parseObject(res, BaseJson.class);

                if (callback != null) {
                    if (mDataJson != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }
}
